package com.uzai.console.service.ks.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.uzai.common.enums.OrderReStatusEnum;
import com.uzai.common.enums.OrderStatusEnum;
import com.uzai.console.common.UzaiRespVo;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.feign.ksorder.KsOrderPrvPullDto;
import com.uzai.console.dto.feign.ksorder.KsOrderSettledDto;
import com.uzai.console.dto.ks.ksorder.KsFindUnReceicedDto;
import com.uzai.console.dto.ks.ksorder.KsOrderDownLoadDto;
import com.uzai.console.dto.ks.kstoken.KsTokenQuery;
import com.uzai.console.dto.locallive.tlbtoken.TlbTokenQuery;
import com.uzai.console.entity.KsOrder;
import com.uzai.console.entity.KsToken;
import com.uzai.console.entity.TlbToken;
import com.uzai.console.mapper.KsOrderMapper;
import com.uzai.console.mapper.KsTokenMapper;
import com.uzai.console.mapper.TlbTokenMapper;
import com.uzai.console.para.KsOrderApiRemoteApiPara;
import com.uzai.console.para.KsOrderRemoteApiPara;
import com.uzai.console.service.feign.KsOrderFeignService;
import com.uzai.console.service.ks.KsOrderSyncService;
import com.uzai.console.service.ks.KsTokenService;
import com.uzai.console.service.remoteapi.KsOrderRemoteApiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: liuqi
 * @Date: 2019/11/27 11:23
 * @Description:
 */
@Service
public class KsOrderSyncServiceImpl implements KsOrderSyncService {

    private static Logger logger = LoggerFactory.getLogger(KsOrderSyncServiceImpl.class);

    @Autowired
    private KsTokenMapper ksTokenMapper;
    @Autowired
    private TlbTokenMapper tlbTokenMapper;
    @Autowired
    private KsOrderMapper ksOrderMapper;
    @Autowired
    private KsOrderFeignService ksOrderFeignService;
    @Autowired
    private KsTokenService ksTokenService;
    @Autowired
    private KsOrderRemoteApiService ksOrderRemoteApiService;

    /**
     * 手动下载单个快手联盟订单
     *
     * @param ksOrderDownLoadDto
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    @Override
    public void downloadOrder(KsOrderDownLoadDto ksOrderDownLoadDto, Long ksTokenId) {
        //快手token表主键ID列表
        if (ksOrderDownLoadDto.getType() == null) {
            return;
        }

        //调用订单业务库服务器
        if (ksOrderDownLoadDto.getType().intValue() == 0) { //自营达人账号
            KsToken ksToken = ksTokenMapper.selectById(ksTokenId, ksOrderDownLoadDto.getMerId());
            if (ksToken != null) {
                if (Tools.getInteger(ksToken.getType()).intValue() == 0) {//cookie模式
                    KsOrderPrvPullDto ksOrderPrvPullDto = new KsOrderPrvPullDto();
                    ksOrderPrvPullDto.setMerId(ksToken.getMerId());
                    ksOrderPrvPullDto.setAppId(ksToken.getAppId());
                    ksOrderPrvPullDto.setStartTime(ksOrderDownLoadDto.getQueryStartTime());
                    ksOrderPrvPullDto.setEndTime(ksOrderDownLoadDto.getQueryEndTime());
                    //订单下载状态(0已付款 1已收货 2订单失效 3订单失效 4退货退款)
                    Integer downloadOrdersStatus = Tools.getInteger(ksOrderDownLoadDto.getDownloadOrderStatus());
                    if (downloadOrdersStatus.intValue() == 0) {//已付款
                        //订单状态  PAY_SUCC 付款、CONFIRM 收货、SETTLE 结算、REFUND 退款
                        ksOrderPrvPullDto.setOrderStatus("PAY_SUCC");
                    } else if (downloadOrdersStatus.intValue() == 1) { //已收货
                        ksOrderPrvPullDto.setOrderStatus("CONFIRM");
                    } else if (downloadOrdersStatus.intValue() == 2) { //订单失效
                        ksOrderPrvPullDto.setOrderStatus("REFUND");
                    } else if (downloadOrdersStatus.intValue() == 3) {
                        ksOrderPrvPullDto.setOrderStatus("REFUND");
                    } else if (downloadOrdersStatus.intValue() == 4) {
                        ksOrderPrvPullDto.setOrderStatus("REFUND");
                    } else {
                        ksOrderPrvPullDto.setOrderStatus("SETTLE");
                    }
                    //页面查询时间类型（4订单付款时间， 5订单完成（收货）时间， 6订单结算时间
                    Integer queryTimeType = Tools.getInteger(ksOrderDownLoadDto.getQueryTimeType());
                    if (queryTimeType == 7) {//更新时间条件缓存付款时间
                        queryTimeType = 4;
                    }
                    //调用下载接口参数时间类型：1付款时间、2收货时间、3结算时间
                    Integer timeType = queryTimeType - 3;
                    if (timeType.intValue() < 1 || timeType.intValue() > 3) {
                        timeType = 1;
                    }
                    ksOrderPrvPullDto.setTimeType(timeType);
                    //请求订单拉取接口
                    UzaiRespVo<Boolean> uzaiRespVo = ksOrderFeignService.prvPull(ksOrderPrvPullDto);
                    logger.info("---调用快手自营联盟下载订单----para={},result={}", JSONObject.toJSONString(ksOrderPrvPullDto), JSONObject.toJSONString(uzaiRespVo));
                } else {//官方api联盟模式
                    KsOrderApiRemoteApiPara ksOrderApiRemoteApiPara = new KsOrderApiRemoteApiPara();
                    ksOrderApiRemoteApiPara.setAccessToken(ksToken.getAccessToken());
                    ksOrderApiRemoteApiPara.setBeginTime(Long.valueOf(ksOrderDownLoadDto.getQueryStartTime()));
                    ksOrderApiRemoteApiPara.setEndTime(Long.valueOf(ksOrderDownLoadDto.getQueryEndTime()));
                    ksOrderApiRemoteApiPara.setChildOrderNo(ksOrderDownLoadDto.getChildOrderNo());
                    //页面查询时间类型（4订单付款时间， 5订单完成（收货）时间， 6订单结算时间
                    Integer queryTimeType = Tools.getInteger(ksOrderDownLoadDto.getQueryTimeType());
                    Integer queryType = 4;   //订单时间查询类型(1-付款时间 4-更新时间)
                    if (queryTimeType == 4) {
                        queryType = 1;
                    }
                    ksOrderApiRemoteApiPara.setQueryType(queryType);
                    ksOrderRemoteApiService.remoteDownloadApi(ksOrderApiRemoteApiPara);
                    logger.info("---调用快手订单库联盟下载订单----para={}", JSONObject.toJSONString(ksOrderApiRemoteApiPara));
                }
            }
        } else if (ksOrderDownLoadDto.getType().intValue() == 1) { //官方共享账号（推老板）
            TlbToken tlbToken = tlbTokenMapper.selectById(ksTokenId, ksOrderDownLoadDto.getMerId());
            if (tlbToken != null) {
                KsOrderRemoteApiPara ksOrderRemoteApiPara = new KsOrderRemoteApiPara();
                ksOrderRemoteApiPara.setAppId(tlbToken.getAppId());
                ksOrderRemoteApiPara.setQueryStartTime(Long.valueOf(ksOrderDownLoadDto.getQueryStartTime()) * 1000L);
                ksOrderRemoteApiPara.setQueryEndTime(Long.valueOf(ksOrderDownLoadDto.getQueryEndTime()) * 1000L);
                ksOrderRemoteApiPara.setChildOrderNo(ksOrderDownLoadDto.getChildOrderNo());
                ksOrderRemoteApiPara.setQueryTimeType(ksOrderDownLoadDto.getQueryTimeType());
                ksOrderRemoteApiPara.setStatus(ksOrderDownLoadDto.getDownloadOrderStatus());
                ksOrderRemoteApiService.remoteDownload(ksOrderRemoteApiPara);
                logger.info("---调用快手官方（推老板）联盟下载订单----para={}", JSONObject.toJSONString(ksOrderRemoteApiPara));
            }
        }
    }

    /**
     * 异步手动下载多个快手联盟订单
     *
     * @param ksOrderDownLoadDto
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    @Async("taskExecutor")
    @Override
    public void downloadOrderListSync(KsOrderDownLoadDto ksOrderDownLoadDto) {
        //快手token表主键ID列表
        if (ksOrderDownLoadDto.getType() == null) {
            return;
        }
        if (ksOrderDownLoadDto.getIdList() != null && ksOrderDownLoadDto.getIdList().size() > 0) {
            for (Long id : ksOrderDownLoadDto.getIdList()) {
                try {
                    downloadOrder(ksOrderDownLoadDto, id);
                } catch (Exception e) {
                    logger.error("-------downloadOrderListSync is error ksTokenId={}, error={}", id, e.getMessage());
                }
            }
        }
    }

    /**
     * 远程调用订单库API，手动更新单个订单
     *
     * @param ksOrder
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    @Async("taskExecutor")
    @Override
    public void updateKsOrder(KsOrder ksOrder) {
        //调用快手共享联盟更新订单接口
        TlbTokenQuery tlbTokenQuery = new TlbTokenQuery();
        tlbTokenQuery.setMerId(ksOrder.getMerId());
        tlbTokenQuery.setType("ks");
        ArrayList<TlbToken> tlbOrderList = tlbTokenMapper.findByList(tlbTokenQuery);
        if (tlbOrderList != null && tlbOrderList.size() > 0) {
            for (TlbToken tlbToken : tlbOrderList) {
                KsOrderDownLoadDto ksOrderDownLoadDto = new KsOrderDownLoadDto();
                ksOrderDownLoadDto.setMerId(ksOrder.getMerId());
                ksOrderDownLoadDto.setType(1);
                ksOrderDownLoadDto.setQueryTimeType(1); //订单创建
                ksOrderDownLoadDto.setQueryStartTime(ksOrder.getCreatedt() - 1);
                ksOrderDownLoadDto.setQueryEndTime(ksOrder.getCreatedt() + 1);
                ksOrderDownLoadDto.setDownloadOrderStatus(0); //全部状态
                ksOrderDownLoadDto.setChildOrderNo(ksOrder.getChildOrderNo());
                downloadOrder(ksOrderDownLoadDto, tlbToken.getId());
            }
        }

        //调用快手自营联盟更新订单接口
        KsTokenQuery ksTokenQuery = new KsTokenQuery();
        ksTokenQuery.setMerId(ksOrder.getMerId());
        ArrayList<KsToken> ksOrderList = ksTokenMapper.findByList(ksTokenQuery);
        if (ksOrderList != null && ksOrderList.size() > 0) {
            for (KsToken ksToken : ksOrderList) {
                KsOrderDownLoadDto ksOrderDownLoadDto = new KsOrderDownLoadDto();
                ksOrderDownLoadDto.setMerId(ksOrder.getMerId());
                ksOrderDownLoadDto.setType(0);
                ksOrderDownLoadDto.setQueryTimeType(4);
                ksOrderDownLoadDto.setQueryStartTime(ksOrder.getPaydt() - 1);
                ksOrderDownLoadDto.setQueryEndTime(ksOrder.getPaydt() + 1);
                ksOrderDownLoadDto.setDownloadOrderStatus(ksOrder.getStatus());
                ksOrderDownLoadDto.setChildOrderNo(ksOrder.getChildOrderNo());
                downloadOrder(ksOrderDownLoadDto, ksToken.getId());
            }
        }
    }

    /**
     * 远程调用订单库API，更新7天前未收货订单
     *
     * @param merId
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    @Async("taskExecutor")
    @Override
    public void updateKsOrderUnReceivedListSync(Long merId) {

        //查询7天前未收货订单
        KsFindUnReceicedDto ksFindUnReceicedDto = new KsFindUnReceicedDto();
        ksFindUnReceicedDto.setMerId(merId);
        //设置开始时间为90天前
        Integer now = DateUtil.getNowTime();
        ksFindUnReceicedDto.setQueryStartTime(now - 7776000);
        //设置结束时间为7天前
        ksFindUnReceicedDto.setQueryEndTime(now - 604800);
        //查询付款状态订单（未收货）
        ksFindUnReceicedDto.setStatus(0);

        List<KsOrder> ksOrderList = ksOrderMapper.findUnReceivedList(ksFindUnReceicedDto);
        if (ksOrderList != null) {
            for (KsOrder ksOrder : ksOrderList) {
                try {
                    updateKsOrder(ksOrder);
                } catch (Exception e) {
                    logger.error("-------updateKsOrderUnReceivedList is error ordersn={}, error={}", ksOrder.getParentOrderNo(), e.getMessage());
                    continue;
                }
            }
        }

    }

    @Override
    public void uploadKsOrderSettingSync(Long merId, int day, int status, List<Integer> sourceStatus) {
        if (merId == null) {
            return;
        }
        //设置结束时间为day天前
        int endTime = DateUtil.getNowTime() - day * 24 * 3600;
        ksOrderMapper.updateStatusPayCompleteToSettle(null, endTime, null, merId, status, sourceStatus);
    }

    /**
     * 远程调用订单库API，更新day天前未收货订单
     *
     * @param merId
     * @param day
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    @Override
    public void updateKsOrderUnReceivedListSync(Long merId, int day) {
        if (merId == null) {
            return;
        }
        //查询7天前未收货订单
        KsFindUnReceicedDto ksFindUnReceicedDto = new KsFindUnReceicedDto();
        ksFindUnReceicedDto.setMerId(merId);
        //设置开始时间未90天前
        Integer now = DateUtil.getNowTime();
        ksFindUnReceicedDto.setQueryStartTime(now - 7776000);
        //设置结束时间为day天前
        int endTime = day * 24 * 3600;
        ksFindUnReceicedDto.setQueryEndTime(now - endTime);
        //查询付款状态订单（未收货）
        ksFindUnReceicedDto.setStatus(0);
        List<KsOrder> ksOrderList = ksOrderMapper.findUnReceivedList(ksFindUnReceicedDto);
        if (ksOrderList != null) {
            for (KsOrder ksOrder : ksOrderList) {
                try {
                    updateKsOrder(ksOrder);
                } catch (Exception e) {
                    logger.error("-------updateKsOrderUnReceivedListSync is error ordersn={}, error={}", ksOrder.getParentOrderNo(), e.getMessage());
                    continue;
                }
            }
        }
    }

    /**
     * 远程调用订单库API，更新day天前已冻结的订单
     *
     * @param merId
     * @param day
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    @Override
    public void updateKsOrderFreezedListSync(Long merId, int day) {
        if (merId == null) {
            return;
        }
        //查询7天前未收货订单
        KsFindUnReceicedDto ksFindUnReceicedDto = new KsFindUnReceicedDto();
        ksFindUnReceicedDto.setMerId(merId);
        //设置开始时间未90天前
        Integer now = DateUtil.getNowTime();
        ksFindUnReceicedDto.setQueryStartTime(now - 7776000);
        //设置结束时间为day天前
        int endTime = day * 24 * 3600;
        ksFindUnReceicedDto.setQueryEndTime(now - endTime);
        //查询付款状态订单（已收货）
        ksFindUnReceicedDto.setStatus(1);
        List<KsOrder> ksOrderList = ksOrderMapper.findFreezedList(ksFindUnReceicedDto);
        if (ksOrderList != null) {
            for (KsOrder ksOrder : ksOrderList) {
                try {
                    updateKsOrder(ksOrder);
                } catch (Exception e) {
                    logger.error("-------updateKsOrderFreezedListSync is error ordersn={}, error={}", ksOrder.getParentOrderNo(), e.getMessage());
                    continue;
                }
            }
        }
    }

    /**
     * 远程调用订单库API，更新day天前未收货订单
     *
     * @param merId
     * @param startTime
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    @Override
    public void updateKsOrderUnReceivedListSync(Long merId, Integer startTime, Integer endTime, Long userId) {
        if (merId == null) {
            return;
        }
        //查询7天前未收货订单
        KsFindUnReceicedDto ksFindUnReceicedDto = new KsFindUnReceicedDto();
        ksFindUnReceicedDto.setMerId(merId);
        ksFindUnReceicedDto.setQueryStartTime(startTime);
        ksFindUnReceicedDto.setQueryEndTime(endTime);
        ksFindUnReceicedDto.setUserId(userId);
        //查询付款状态订单（未收货）
        ksFindUnReceicedDto.setStatus(0);
        List<KsOrder> ksOrderList = ksOrderMapper.findUnReceivedList(ksFindUnReceicedDto);
        if (ksOrderList != null) {
            for (KsOrder ksOrder : ksOrderList) {
                try {
                    updateKsOrder(ksOrder);
                } catch (Exception e) {
                    logger.error("-------updateKsOrderUnReceivedListSync is error ordersn={}, error={}", ksOrder.getParentOrderNo(), e.getMessage());
                    continue;
                }
            }
        }
    }

    /**
     * 直接结算，更新day天前未收货订单
     *
     * @param merId
     * @param startTime
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    @Override
    public void updateKsOrderUnReceivedListSync_settle(Long merId, Integer startTime, Integer endTime, Long userId) {
        if (merId == null) {
            return;
        }
        //查询7天前未收货订单
        KsFindUnReceicedDto ksFindUnReceicedDto = new KsFindUnReceicedDto();
        ksFindUnReceicedDto.setMerId(merId);
        ksFindUnReceicedDto.setQueryStartTime(startTime);
        ksFindUnReceicedDto.setQueryEndTime(endTime);
        ksFindUnReceicedDto.setUserId(userId);
        //查询付款状态订单（未收货）
        ksFindUnReceicedDto.setStatus(0);
        List<KsOrder> ksOrderList = ksOrderMapper.findUnReceivedList(ksFindUnReceicedDto);
        if (ksOrderList != null) {
            for (KsOrder ksOrder : ksOrderList) {
                try {
                    //订单状态为0-付款状态时，才能确认收货
                    if (ksOrder.getStatus() == 0) {
                        //调用确认收货接口
                        KsOrderSettledDto ksOrderSettledDto = new KsOrderSettledDto();
                        ksOrderSettledDto.setMerId(ksOrder.getMerId());
                        ksOrderSettledDto.setOrderId(ksOrder.getParentOrderNo());
                        ksOrderSettledDto.setChildOrderId(ksOrder.getChildOrderNo());
                        UzaiRespVo<Object> result = ksOrderFeignService.settled(ksOrderSettledDto);
                        logger.info("---发送修改快手确认收货-para={}, result={}", JSONObject.toJSONString(ksOrderSettledDto), JSONObject.toJSONString(result));
                    }
                } catch (Exception e) {
                    logger.error("-------updateKsOrderUnReceivedListSync_settle is error ordersn={}, error={}", ksOrder.getParentOrderNo(), e.getMessage());
                    continue;
                }
            }
        }
    }

    /**
     * 远程调用订单库API，更新day天前已冻结的订单
     *
     * @param merId
     * @param startTime
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    @Override
    public void updateKsOrderFreezedListSync(Long merId, Integer startTime, Integer endTime, Long userId) {
        if (merId == null) {
            return;
        }
        //查询7天前未收货订单
        KsFindUnReceicedDto ksFindUnReceicedDto = new KsFindUnReceicedDto();
        ksFindUnReceicedDto.setMerId(merId);
        ksFindUnReceicedDto.setQueryStartTime(startTime);
        ksFindUnReceicedDto.setQueryEndTime(endTime);
        ksFindUnReceicedDto.setUserId(userId);
        //查询付款状态订单（已收货）
        ksFindUnReceicedDto.setStatus(1);
        List<KsOrder> ksOrderList = ksOrderMapper.findFreezedList(ksFindUnReceicedDto);
        if (ksOrderList != null) {
            for (KsOrder ksOrder : ksOrderList) {
                try {
                    updateKsOrder(ksOrder);
                } catch (Exception e) {
                    logger.error("-------updateKsOrderFreezedListSync is error ordersn={}, error={}", ksOrder.getParentOrderNo(), e.getMessage());
                    continue;
                }
            }
        }
    }

    /**
     * 直接结算，更新day天前已冻结订单
     *
     * @param merId
     * @param startTime
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    @Override
    public void updateKsOrderFreezedListSync_settle(Long merId, Integer startTime, Integer endTime, Long userId) {
        if (merId == null) {
            return;
        }
        //查询7天前未收货订单
        KsFindUnReceicedDto ksFindUnReceicedDto = new KsFindUnReceicedDto();
        ksFindUnReceicedDto.setMerId(merId);
        ksFindUnReceicedDto.setQueryStartTime(startTime);
        ksFindUnReceicedDto.setQueryEndTime(endTime);
        ksFindUnReceicedDto.setUserId(userId);
        //查询付款状态订单（已收货）
        ksFindUnReceicedDto.setStatus(1);
        List<KsOrder> ksOrderList = ksOrderMapper.findFreezedList(ksFindUnReceicedDto);
        if (ksOrderList != null) {
            for (KsOrder ksOrder : ksOrderList) {
                try {
                    //先修改订单状态为0-付款状态时，才能确认收货
                    ksOrder.setStatus(OrderStatusEnum.STATUS_PAID_VALUE);
                    ksOrder.setUpdateTime(DateUtil.getNowTime());
                    ksOrderMapper.updateStatus(ksOrder);
                    //调用确认收货接口
                    KsOrderSettledDto ksOrderSettledDto = new KsOrderSettledDto();
                    ksOrderSettledDto.setMerId(ksOrder.getMerId());
                    ksOrderSettledDto.setOrderId(ksOrder.getParentOrderNo());
                    ksOrderSettledDto.setChildOrderId(ksOrder.getChildOrderNo());
                    UzaiRespVo<Object> result = ksOrderFeignService.settled(ksOrderSettledDto);
                    logger.info("---发送修改快手确认收货-para={}, result={}", JSONObject.toJSONString(ksOrderSettledDto), JSONObject.toJSONString(result));
                } catch (Exception e) {
                    logger.error("-------updateKsOrderFreezedListSync_settle is error ordersn={}, error={}", ksOrder.getParentOrderNo(), e.getMessage());
                    continue;
                }
            }
        }
    }

    /**
     * 远程调用订单库API，更新day天前维权中订单
     *
     * @param merId
     * @param day
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    @Override
    public void updateKsOrderRefundingListSync(Long merId, int day) {
        if (merId == null) {
            return;
        }
        //查询7天前未收货订单
        KsFindUnReceicedDto ksFindUnReceicedDto = new KsFindUnReceicedDto();
        ksFindUnReceicedDto.setMerId(merId);
        //设置开始时间未90天前
        Integer now = DateUtil.getNowTime();
        ksFindUnReceicedDto.setQueryStartTime(now - 7776000);
        //设置结束时间为day天前
        int endTime = day * 24 * 3600;
        ksFindUnReceicedDto.setQueryEndTime(now - endTime);
        //查询付款状态订单（未收货）
        ksFindUnReceicedDto.setStatus(OrderStatusEnum.STATUS_REFUNDING_VALUE);
        List<KsOrder> ksOrderList = ksOrderMapper.findUnReceivedList(ksFindUnReceicedDto);
        if (ksOrderList != null) {
            for (KsOrder ksOrder : ksOrderList) {
                try {
                    updateKsOrder(ksOrder);
                } catch (Exception e) {
                    logger.error("-------updateKsOrderRefundingListSync is error ordersn={}, error={}", ksOrder.getParentOrderNo(), e.getMessage());
                    continue;
                }
            }
        }
    }

    /**
     * 远程调用订单库API，更新day天前维权中的订单
     *
     * @param merId
     * @param startTime
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    public void updateKsOrderRefundingListSync(Long merId, Integer startTime, Integer endTime, Long userId) {
        if (merId == null) {
            return;
        }
        //查询7天前未收货订单
        KsFindUnReceicedDto ksFindUnReceicedDto = new KsFindUnReceicedDto();
        ksFindUnReceicedDto.setMerId(merId);
        ksFindUnReceicedDto.setQueryStartTime(startTime);
        ksFindUnReceicedDto.setQueryEndTime(endTime);
        ksFindUnReceicedDto.setUserId(userId);
        //查询付款状态订单（未收货）
        ksFindUnReceicedDto.setStatus(OrderStatusEnum.STATUS_REFUNDING_VALUE);
        List<KsOrder> ksOrderList = ksOrderMapper.findUnReceivedList(ksFindUnReceicedDto);
        if (ksOrderList != null) {
            for (KsOrder ksOrder : ksOrderList) {
                try {
                    updateKsOrder(ksOrder);
                } catch (Exception e) {
                    logger.error("-------updateKsOrderRefundingListSync is error ordersn={}, error={}", ksOrder.getParentOrderNo(), e.getMessage());
                    continue;
                }
            }
        }
    }

    /**
     * 直接结算，更新day天前维权中订单
     *
     * @param merId
     * @param startTime
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    public void updateKsOrderRefundingListSync_settle(Long merId, Integer startTime, Integer endTime, Long userId) {
        if (merId == null) {
            return;
        }
        //查询7天前维权中订单
        KsFindUnReceicedDto ksFindUnReceicedDto = new KsFindUnReceicedDto();
        ksFindUnReceicedDto.setMerId(merId);
        ksFindUnReceicedDto.setQueryStartTime(startTime);
        ksFindUnReceicedDto.setQueryEndTime(endTime);
        ksFindUnReceicedDto.setUserId(userId);
        //查询付款状态订单（未收货）
        ksFindUnReceicedDto.setStatus(OrderStatusEnum.STATUS_REFUNDING_VALUE);
        //根据订单返利状态查询
        ksFindUnReceicedDto.setReStatusList(Lists.newArrayList(OrderReStatusEnum.RE_STATUS_NO_VALUE, OrderReStatusEnum.RE_STATUS_FROZEN_VALUE));

        List<KsOrder> ksOrderList = ksOrderMapper.findUnReceivedList(ksFindUnReceicedDto);
        if (ksOrderList != null) {
            for (KsOrder ksOrder : ksOrderList) {
                try {
                    //先修改订单状态为0-付款状态时，才能确认收货
                    ksOrder.setStatus(OrderStatusEnum.STATUS_PAID_VALUE);
                    ksOrder.setUpdateTime(DateUtil.getNowTime());
                    ksOrderMapper.updateStatus(ksOrder);
                    //调用确认收货接口
                    KsOrderSettledDto ksOrderSettledDto = new KsOrderSettledDto();
                    ksOrderSettledDto.setMerId(ksOrder.getMerId());
                    ksOrderSettledDto.setOrderId(ksOrder.getParentOrderNo());
                    ksOrderSettledDto.setChildOrderId(ksOrder.getChildOrderNo());
                    UzaiRespVo<Object> result = ksOrderFeignService.settled(ksOrderSettledDto);
                    logger.info("---发送修改快手确认收货-para={}, result={}", JSONObject.toJSONString(ksOrderSettledDto), JSONObject.toJSONString(result));
                } catch (Exception e) {
                    logger.error("-------updateKsOrderRefundingListSync_settle is error ordersn={}, error={}", ksOrder.getParentOrderNo(), e.getMessage());
                    continue;
                }
            }
        }
    }
}
